package cn.zh.stan.activity;

import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import com.lzy.okhttputils.OkHttpUtils;
import com.lzy.okhttputils.callback.StringCallback;

import org.json.JSONException;
import org.json.JSONObject;

import cn.zh.stan.BaseUrl;
import cn.zh.stan.MyApplication;
import cn.zh.stan.R;
import cn.zh.stan.util.ToastUtil;
import okhttp3.Call;
import okhttp3.Request;
import okhttp3.Response;

public class FeedBack extends BaseActivity {
    ImageView  rebackf;
    Button submit;
    EditText txtent;
    MyApplication mapp;
    String uid;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_re_back);
        mapp= (MyApplication) getApplication();
        uid=mapp.getSp().getString("uid","");
        initf();

         }

      public void initf(){
          rebackf= (ImageView) findViewById(R.id.rebackf);
          submit= (Button) findViewById(R.id.submit);
          txtent= (EditText) findViewById(R.id.txtent);
          rebackf.setOnClickListener(this);
          submit.setOnClickListener(this);

          }




    @Override
    public void onClick(View view) {
        super.onClick(view);
        int id=view.getId();
        switch (id){
            case R.id.rebackf:
                finish();
                break;
            case R.id.submit:
                subfeedback();
                break;
            }
          }


        public void subfeedback(){
          final String body=txtent.getText().toString();
            OkHttpUtils.post(BaseUrl.FeedBack)
                    .params("userId",uid)
                    .params("body",body)
                    .execute(new StringCallback() {
                        @Override
                        public void onResponse(boolean isFromCache, String s, Request request, @Nullable Response response) {
                            System.out.println("----->提交用户资料==="+s);
                            try {
                                JSONObject js=new JSONObject(s);
                                boolean State=js.getBoolean("State");
                                if (State){
                                    boolean Flag=js.getBoolean("Flag");
                                    if (Flag){
                                        String Msg=js.getString("Msg");
                                        ToastUtil.showLong(FeedBack.this,Msg);
                                        finish();
                                      }else {
                                        String Msg=js.getString("Msg");
                                        ToastUtil.showLong(FeedBack.this,Msg);
                                    }
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                        @Override
                        public void onError(boolean isFromCache, Call call, @Nullable Response response, @Nullable Exception e) {
                            super.onError(isFromCache, call, response, e);
                        }
                    });



        }

}
